import configparser
import ctypes
import os
from ctypes import wintypes
import cv2
import numpy as np
from PIL import Image, ImageWin, ImageDraw

# 定义 ctypes 结构体
class POINT(ctypes.Structure):
    _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]

class RECT(ctypes.Structure):
    _fields_ = [("left", ctypes.c_long), ("top", ctypes.c_long),
                ("right", ctypes.c_long), ("bottom", ctypes.c_long)]

class BITMAPINFOHEADER(ctypes.Structure):
    _fields_ = [("biSize", wintypes.DWORD),
                ("biWidth", wintypes.LONG),
                ("biHeight", wintypes.LONG),
                ("biPlanes", wintypes.WORD),
                ("biBitCount", wintypes.WORD),
                ("biCompression", wintypes.DWORD),
                ("biSizeImage", wintypes.DWORD),
                ("biXPelsPerMeter", wintypes.LONG),
                ("biYPelsPerMeter", wintypes.LONG),
                ("biClrUsed", wintypes.DWORD),
                ("biClrImportant", wintypes.DWORD)]

class BITMAPINFO(ctypes.Structure):
    _fields_ = [("bmiHeader", BITMAPINFOHEADER),
                ("bmiColors", ctypes.c_ubyte * 3)]

# 获取屏幕设备上下文
def get_screen_dc():
    return ctypes.windll.user32.GetDC(None)

# 释放屏幕设备上下文
def release_screen_dc(hdc):
    ctypes.windll.user32.ReleaseDC(None, hdc)

# 将 BGR 颜色转换为 COLORREF
def bgr_to_colorref(bgr_color):
    r, g, b = bgr_color
    return b | (g << 8) | (r << 16)

# 在屏幕上绘制像素
def draw_pixel(hdc, x, y, color):
    colorref = bgr_to_colorref(color)
    ctypes.windll.gdi32.SetPixel(hdc, x, y, colorref)

# 在屏幕上绘制十字准星
def draw_crosshair(hdc, screen_width, screen_height, crosshair_color, crosshair_size, crosshair_thickness, is_visible, image=None):
    center_x, center_y = screen_width // 2, screen_height // 2

    if is_visible:
        half_thickness = crosshair_thickness // 2

        # 绘制水平线
        for i in range(center_x - crosshair_size, center_x + crosshair_size + 1):
            for t in range(-half_thickness, half_thickness + 1):
                if image is not None:
                    image[center_y + t, i, :3] = crosshair_color
                    image[center_y + t, i, 3] = 255  # 设置透明度
                else:
                    draw_pixel(hdc, i, center_y + t, crosshair_color)

        # 绘制垂直线
        for i in range(center_y - crosshair_size, center_y + crosshair_size + 1):
            for t in range(-half_thickness, half_thickness + 1):
                if image is not None:
                    image[i, center_x + t, :3] = crosshair_color
                    image[i, center_x + t, 3] = 255  # 设置透明度
                else:
                    draw_pixel(hdc, center_x + t, i, crosshair_color)

# 在屏幕上绘制“A”字准星
def draw_a_shaped_crosshair(hdc, screen_width, screen_height, crosshair_color, crosshair_size, crosshair_thickness, is_visible, image=None):
    center_x, center_y = screen_width // 2, screen_height // 2

    if is_visible:
        half_thickness = crosshair_thickness // 2

        # 绘制“A”字准星的左斜线
        for i in range(0, crosshair_size + 1):
            for t in range(-half_thickness, half_thickness + 1):
                if image is not None:
                    image[center_y + i + t, center_x - i, :3] = crosshair_color
                    image[center_y + i + t, center_x - i, 3] = 255  # 设置透明度
                else:
                    draw_pixel(hdc, center_x - i, center_y + i + t, crosshair_color)

        # 绘制“A”字准星的右斜线
        for i in range(0, crosshair_size + 1):
            for t in range(-half_thickness, half_thickness + 1):
                if image is not None:
                    image[center_y + i + t, center_x + i, :3] = crosshair_color
                    image[center_y + i + t, center_x + i, 3] = 255  # 设置透明度
                else:
                    draw_pixel(hdc, center_x + i, center_y + i + t, crosshair_color)

# 根据样式绘制准星
def draw_crosshair_by_style(hdc, screen_width, screen_height, crosshair_color, crosshair_size, crosshair_thickness, is_visible, crosshair_style, image=None):
    if crosshair_style == "Cross":
        draw_crosshair(hdc, screen_width, screen_height, crosshair_color, crosshair_size, crosshair_thickness, is_visible, image)
    elif crosshair_style == "Aword":
        draw_a_shaped_crosshair(hdc, screen_width, screen_height, crosshair_color, crosshair_size, crosshair_thickness, is_visible, image)

# 绘制自定义准星
def draw_custom_crosshair(hdc, screen_width, screen_height, custom_crosshair, is_visible, image=None):
    if is_visible and custom_crosshair is not None:
        custom_crosshair_height, custom_crosshair_width = custom_crosshair.shape[:2]
        center_x, center_y = screen_width // 2, screen_height // 2

        # 计算准星在屏幕上的位置
        x_offset = custom_crosshair_width // 2
        y_offset = custom_crosshair_height // 2

        # 将自定义准星绘制到图像中
        if image is not None:
            # 确保准星在屏幕范围内
            start_x = max(0, center_x - x_offset)
            start_y = max(0, center_y - y_offset)
            end_x = min(screen_width, center_x + x_offset)
            end_y = min(screen_height, center_y + y_offset)

            # 计算准星在图像中的位置
            img_start_x = start_x - (center_x - x_offset)
            img_start_y = start_y - (center_y - y_offset)
            img_end_x = img_start_x + (end_x - start_x)
            img_end_y = img_start_y + (end_y - start_y)

            image[start_y:end_y, start_x:end_x] = custom_crosshair[img_start_y:img_end_y, img_start_x:img_end_x]
        else:
            # 使用 GDI 绘制自定义准星到屏幕上
            custom_crosshair_bgra = cv2.cvtColor(custom_crosshair, cv2.COLOR_BGRA2RGBA)
            custom_crosshair_pil = Image.fromarray(custom_crosshair_bgra)
            custom_crosshair_dc = ImageWin.DCFromHandle(hdc)

            # 确保准星在屏幕范围内
            start_x = max(0, center_x - x_offset)
            start_y = max(0, center_y - y_offset)
            end_x = min(screen_width, center_x + x_offset)
            end_y = min(screen_height, center_y + y_offset)

            # 计算准星在图像中的位置
            img_start_x = start_x - (center_x - x_offset)
            img_start_y = start_y - (center_y - y_offset)
            img_end_x = img_start_x + (end_x - start_x)
            img_end_y = img_start_y + (end_y - start_y)

            # 裁剪图像以适应屏幕范围
            custom_crosshair_pil = custom_crosshair_pil.crop((img_start_x, img_start_y, img_end_x, img_end_y))

            # 绘制到屏幕设备上下文
            custom_crosshair_pil.save(custom_crosshair_dc, "PNG")
            custom_crosshair_dc.DeleteDC()
